package cn.xinfei.xdecision.engine.runner.context.model;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

@Slf4j
@Data
public class ContextModel {
    private String nodeCode;
    private Object value;


    public ContextModel(String nodeCode,  Object value) {
        this.nodeCode = nodeCode;
        this.value = value;
    }

   /* @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ContextModel model = (ContextModel) o;
        return nodeCode.equals(model.nodeCode) &&
//                ( (value==null&&model.value==null)||(value!=null&&model.value!=null&&value.equals(model.value))) &&
                value.equals(model.value);
    }

    @Override
    public int hashCode() {
        return Objects.hash(nodeCode, value);
    }

    public boolean identify(ContextModel target) {
        try {
            return target.hashCode() == this.hashCode() && this.equals(target);
        } catch (Exception e) {
            log.warn("identical error.new={},target={}", JsonUtils.toJsonString(this), JsonUtils.toJsonString(target));
        }
        return false;
    }

    public String diff(ContextModel old) {
        StringBuffer sb = new StringBuffer();
        try {
            if (!this.nodeCode.equals(old.nodeCode)) {
                sb.append(old.nodeCode + ":" + this.nodeCode + ",");
            }
            if (!this.value.equals(old.value)) {
                sb.append(old.value + ":" + this.value + ",");
            }
        } catch (Exception e) {
            log.warn("diff error.new={},old={},error={}", JsonUtils.toJsonString(this), JsonUtils.toJsonString(old), e.getMessage());
        }

        return sb.toString();
    }*/
}
